﻿using Delonix_Regia_HMS.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delonix_Regia_HMS
{
    public partial class RoomAvailabilityBooking : Form
    {
        public RoomAvailabilityBooking()
        {
            InitializeComponent();
        }

        DateTime initialCheckInDate = DateTime.MinValue;
        DateTime initialCheckOutDate = DateTime.MinValue;
        DateTime updatedCheckInDate = DateTime.MinValue;
        DateTime updatedCheckOutDate = DateTime.MinValue;
        string initialRoomType = null;
        string updatedRoomType = null;

        //Ensure that they only put the correct numbers/characters for the selected textbox
        void OnlyCharacters_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
        }

        private void onlyNumbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        //Search Booking By Booking Id
        private void btnBookingManagementSearchByBookingId_Click(object sender, EventArgs e)
        {
            tbxBookingManagementSearchNRIC.Clear();
            if (string.IsNullOrEmpty(tbxBookingManagementSearchBookingId.Text))
            {
                MessageBox.Show("Please Enter a Booking ID to Search!");
            }
            else
            {
                DBManager.DisplayBookingsByBookingId(listViewBookingManagementBookingInformation, Convert.ToInt32(tbxBookingManagementSearchBookingId.Text));
            }        
        }

        //Search Booking By Customer NRIC
        private void btnBookingManagementSearchByCustomerNRIC_Click(object sender, EventArgs e)
        {
            tbxBookingManagementSearchBookingId.Clear();
            DBManager.DisplayBookingsByCustomerNRIC(listViewBookingManagementBookingInformation, tbxBookingManagementSearchNRIC.Text);
        }

        //Display All Bookings
        private void btnBookingManagementDisplayAllBookings_Click(object sender, EventArgs e)
        {
            tbxBookingManagementSearchBookingId.Clear();
            tbxBookingManagementSearchNRIC.Clear();
            DBManager.DisplayAllBookings(listViewBookingManagementBookingInformation);
        }

        //Create Bookings
        private void btnBookingManagementCreateBooking_Click(object sender, EventArgs e)
        {
            CreateBooking createBooking = new CreateBooking();
            createBooking.ShowDialog();
        }

        //Update Booking
        private void btnBookingManagementUpdateBooking_Click(object sender, EventArgs e)
        {
            bool successfulUpdate = false;
            if (string.IsNullOrEmpty(tbxBookingManagementBookingId.Text))//Check if there is a booking Id
            {
                MessageBox.Show("Please Select a Booking to Update!");
            }
            else if (string.IsNullOrEmpty(tbxBookingManagementNRIC.Text))
            {
                MessageBox.Show("Please Select a Customer that wants to book!");
            }
            else if (string.IsNullOrEmpty(cbxBookingManagementRoomType.Text))
            {
                MessageBox.Show("Please Select a Room Type!");
            }
            else if (string.IsNullOrEmpty(tbxBookingManagementCheckInTime.Text))
            {
                MessageBox.Show("Please Enter a Check In Time!");
            }
            else if (tbxBookingManagementCheckInTime.TextLength < 4)
            {
                MessageBox.Show("Please Enter a Check In Time in 24 hour format from 0000  to 2359. E.g. If 1am enter 0100");
            }
            else if (Convert.ToInt32(tbxBookingManagementCheckInTime.Text) > 2359 || Convert.ToInt32(tbxBookingManagementCheckInTime.Text) < 0000)
            {
                MessageBox.Show("Please Enter a Check In Time in 24 hour format from 0000  to 2359");
            }
            else if (string.IsNullOrEmpty(tbxBookingManagementCheckOutTime.Text))
            {
                MessageBox.Show("Please Enter a Check Out Time!");
            }
            else if (tbxBookingManagementCheckOutTime.TextLength < 4)
            {
                MessageBox.Show("Please Enter a Check Out Time in 24 hour format) from 1100  to 1200 (11am to 12pm)");
            }
            else if (Convert.ToInt32(tbxBookingManagementCheckOutTime.Text) > 1200 || Convert.ToInt32(tbxBookingManagementCheckOutTime.Text) < 1100)
            {
                MessageBox.Show("Please Enter a Check Out Time in 24 hour format) from 1100  to 1200 (11am to 12pm)");
            }
            else if (string.IsNullOrEmpty(tbxBookingManagementNumberOfAdults.Text))
            {
                MessageBox.Show("Please Enter the number of adults staying in the room. If 0 put 0!");
            }
            else if (string.IsNullOrEmpty(tbxBookingManagementNumberOfAdults.Text))
            {
                MessageBox.Show("Please Enter the number of adult(s) staying in the room. If 0 put 0!");
            }
            else if (string.IsNullOrEmpty(tbxBookingManagementNumberOfChildrens.Text))
            {
                MessageBox.Show("Please Enter the number of children(s) staying in the room. If 0 put 0!");
            }
            else if (string.IsNullOrEmpty(cbxBookingManagementLateCheckOut.Text))
            {
                MessageBox.Show("Please Indicate Whether there is a late checkout!");
            }
            else //  Everything filled up
            {
                #region Check Whether the days between the selected check in date and selected check out date is there any days with 0 rooms available
                bool checkForAvailableRoom = true;
                updatedCheckInDate = Convert.ToDateTime(dtpBookingManagementCheckInDate.Text);
                updatedCheckOutDate = Convert.ToDateTime(dtpBookingManagementCheckOutDate.Text);
                TimeSpan totalBookedDays = updatedCheckOutDate - updatedCheckInDate;
                int iTotalBookedDays = Convert.ToInt32(totalBookedDays.TotalDays);
                for (int i = 0; i < iTotalBookedDays; i++)
                {
                    int numberOfRoomsAvailable = DBManager.CheckRoomAvailabilityForCreateBookingAndUpdateBooking(updatedCheckInDate.AddDays(i), cbxBookingManagementRoomType.Text);
                    if (numberOfRoomsAvailable == 0)
                    {
                        checkForAvailableRoom = false;
                    }
                }
                #endregion
                if (checkForAvailableRoom == true)
                {
                    Booking b = new Booking();
                    b.BookingID = Convert.ToInt32(tbxBookingManagementBookingId.Text);
                    b.Nric = tbxBookingManagementNRIC.Text;
                    b.RoomType = cbxBookingManagementRoomType.Text;
                    updatedCheckInDate = Convert.ToDateTime(dtpBookingManagementCheckInDate.Text);
                    b.CheckInDate = updatedCheckInDate;
                    updatedCheckOutDate = Convert.ToDateTime(dtpBookingManagementCheckOutDate.Text);
                    b.CheckOutDate = updatedCheckOutDate;
                    b.CheckInTime = tbxBookingManagementCheckInTime.Text;
                    b.CheckOutTime = tbxBookingManagementCheckOutTime.Text;
                    b.NumberOfAdults = Convert.ToInt32(tbxBookingManagementNumberOfAdults.Text);
                    b.NumberOfChildrens = Convert.ToInt32(tbxBookingManagementNumberOfChildrens.Text);
                    b.LateCheckOut = cbxBookingManagementLateCheckOut.Text;
                    if (string.IsNullOrEmpty(tbxBookingManagementRemarks.Text))
                    {
                        b.AdditionalRemarks = null;
                    }
                    else
                    {
                        b.AdditionalRemarks = tbxBookingManagementRemarks.Text;
                    }
                    b.BookingStatus = tbxBookingManagementBookingStatus.Text;
                    DBManager.UpdateBooking(b);
                    successfulUpdate = true;
                }
                else
                {
                    MessageBox.Show("Sorry there are no available rooms between the updated check in date and updated check out date!");
                }
                
            }
            if (successfulUpdate == true)
            {
                MessageBox.Show("Update Successful!");
                TimeSpan totalDaysBooked = updatedCheckOutDate - updatedCheckInDate;
                int iTotalDaysBooked = Convert.ToInt32(totalDaysBooked.TotalDays);
                for(int i = 0; i < iTotalDaysBooked; i++)
                {
                    DBManager.UpdateRoomAvailabilityForUpdateBooking(updatedCheckInDate.AddDays(i), initialRoomType, updatedRoomType);
                }
            }
            else
            {
                MessageBox.Show("Update NOT Sucessful!");
            }
        }

        //Selected Booking
        private void listViewBookingInformation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewBookingManagementBookingInformation.SelectedItems.Count > 0)
            {
                foreach (ListViewItem bookingItems in listViewBookingManagementBookingInformation.SelectedItems)
                {
                    tbxBookingManagementBookingId.Text = listViewBookingManagementBookingInformation.SelectedItems[0].Text;
                    tbxBookingManagementNRIC.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[1].Text;
                    initialRoomType = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[2].Text;
                    cbxBookingManagementRoomType.Text = initialRoomType;
                    initialCheckInDate = Convert.ToDateTime(listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[3].Text);
                    initialCheckOutDate = Convert.ToDateTime(listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[4].Text);
                    dtpBookingManagementCheckInDate.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[3].Text;
                    dtpBookingManagementCheckOutDate.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[4].Text;
                    tbxBookingManagementCheckInTime.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[5].Text;
                    tbxBookingManagementCheckOutTime.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[6].Text;
                    tbxBookingManagementNumberOfAdults.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[7].Text;
                    tbxBookingManagementNumberOfChildrens.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[8].Text;
                    cbxBookingManagementLateCheckOut.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[9].Text;
                    tbxBookingManagementBookingStatus.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[10].Text;
                    tbxBookingManagementRemarks.Text = listViewBookingManagementBookingInformation.SelectedItems[0].SubItems[11].Text;
                }
            }
        }

        //Delete Booking
        private void btnBookingManagementDeleteBooking_Click(object sender, EventArgs e)
        {
            bool successfulDelete = false;
            if (string.IsNullOrEmpty(tbxBookingManagementBookingId.Text))
            {
                MessageBox.Show("Please Select a Booking to Delete!");
            }
            else
            {
                DBManager.DeleteBooking(Convert.ToInt32(tbxBookingManagementBookingId.Text));
                successfulDelete = true;  
            }
            if (successfulDelete == true)
            {
                MessageBox.Show("Delete Booking Success!");
                TimeSpan totalDaysBooked = initialCheckOutDate - initialCheckInDate;
                int iTotalDaysBooked = Convert.ToInt32(totalDaysBooked.TotalDays);
                for(int i = 0; i < iTotalDaysBooked; i++)
                {
                    DBManager.UpdateRoomAvailabilityForDeleteBooking(initialCheckInDate.AddDays(i), initialRoomType);
                }
            }
            else
            {
                MessageBox.Show("Delete Booking NOT Successful!");
            }
        }

        //Clear Customer Nric textbox when user search by booking id
        private void tbxBookingManagementSearchBookingId_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxBookingManagementSearchBookingId.Text))
            {
                tbxBookingManagementSearchNRIC.Clear();
                tbxBookingManagementSearchBookingId.Text = ((TextBox)sender).Text;
            }
            else if (!string.IsNullOrEmpty(tbxBookingManagementSearchNRIC.Text))
            {
                tbxBookingManagementSearchBookingId.Clear();
                tbxBookingManagementSearchNRIC.Text = ((TextBox)sender).Text;
            }
        }

        //Clear booking id textbox when user search by customer nric
        private void tbxBookingManagementSearchNRIC_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxBookingManagementSearchNRIC.Text))
            {
                tbxBookingManagementSearchBookingId.Clear();
                tbxBookingManagementSearchNRIC.Text = ((TextBox)sender).Text;
            }
            else if (!string.IsNullOrEmpty(tbxBookingManagementSearchBookingId.Text))
            {
                tbxBookingManagementSearchNRIC.Clear();
                tbxBookingManagementSearchBookingId.Text = ((TextBox)sender).Text;
            }
        }

        private void btnBookingManagementSearchByCustomerName_Click(object sender, EventArgs e)
        {
            DBManager.DisplayBookingsByCustomerFirstName(listViewBookingManagementBookingInformation, tbxBookingManagementSearchCustomerName.Text);
        }

        private void cbxBookingManagementRoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            updatedRoomType = cbxBookingManagementRoomType.Text;
        }
    }
}
